<?php

defined ('_JEXEC') or die();
jimport( 'joomla.application.component.model' );

class SchedulersModelSchedulers extends JModel {

	var $_data;
	var $scheduler_lang;

	function getLangs() {
		$document = & JFactory::getDocument();
		$locale = substr($document->language,0 ,2);
		$lang_path = JPATH_COMPONENT.DS.'sources'.DS.$locale.'.php';
		$lang_path = str_replace("administrator".DS, "", $lang_path);
		if (file_exists($lang_path)) {
			include($lang_path);
		} else {
			$lang_path = JPATH_COMPONENT.DS.'sources'.DS.'en.php';
			$lang_path = str_replace("administrator".DS, "", $lang_path);
			include($lang_path);
		}
		$this->lang_name = $locale;
		$this->scheduler_lang = $scheduler_lang;
	}

	function getData() {
		$this->getLangs();
		$query = 'SELECT * FROM #__scheduler_options ORDER BY `order`';		
		
		$this->_db->setQuery($query);
		$this->_data = $this->_db->loadObjectList();
		for ($i = 0; $i < count($this->_data); $i++) {
			$row = $this->_data[$i];
			
			switch ($row->type) {
				case 'text':
					$element = $this->text_create($row);
					break;
				case 'checkbox':
					$element = $this->checkbox_create($row);
					break;
				case 'radio':
					$element = $this->radio_create($row);
					break;
				case 'select':
					$element = $this->select_create($row);
					break;
				case 'space':
					$element = $this->space_create($row);
					break;
				default: 
					$element = 'Not found!';
				}
			
			$options[$i]['element'] = $this->lang_replacer($element);
			if ($row->label == '') {
				$options[$i]['label'] = '&nbsp;';
				}
				else {
				$options[$i]['label'] = $this->lang_replacer($row->label);
				}
				
			$options[$i]['dsc'] = $this->lang_replacer($row->dsc);
			}
		$config =& JFactory::getConfig();
		if ($config->_registry['config']['data']->scheduler_log == true) {
			error_log("SchedulersModelSchedulers getData\r\ndata = ".print_r($this->_data, true)."\r\n\r\n", 3, JPATH_ROOT."/scheduler.log");
		}
		return $options;
		}
		
		
	function store($data) {
		$row = $this->getTable('Scheduler', 'Table');
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
			}

		if (!$row->check()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
			}

		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
			}
	
	return true;
	}
	
	function text_create($row) {
		$result = '<input type="text" name="cfg_'.$row->name.'" value="'.$row->value.'" size="'.$row->size.'" maxlength="'.$row->maxlength.'" />';
		return $result;
		}
	
	function radio_create($row) {
		$result = '';
		$options_value = explode('|', $row->options);
		$options_label = explode('|', $row->options_label);
		for ($i = 0; $i< count($options_value); $i++) {
			if ($options_value[$i] == $row->value) {
				$sel = ' checked="checked" ';
				}
				else {
				$sel = '';
				}
			$result .= '<input name="cfg_'.$row->name.'" id="'.$row->name.$i.'" value="'.$options_value[$i].'" type="radio"'.$sel.'><label for="'.$row->name.$i.'">'.$options_label[$i].'</label>';
			}
		return $result;
		}
		
	function checkbox_create($row) {
		$result = '<input type="checkbox" name="cfg_'.$row->name.'" ';
		if ($row->value == '1') {
			$result .= 'checked="checked" ';
			}
		$result .= ' />';
		return $result;
		}
		
	function select_create($row) {
		$result = '<select name="cfg_'.$row->name.'" id="'.$row->name.'">';
		$options_value = explode('|', $row->options);
		$options_label = explode('|', $row->options_label);
		for ($i = 0; $i< count($options_value); $i++) {
			if ($options_value[$i] == $row->value) {
				$sel = ' selected="selected" ';
				}
				else {
				$sel = '';
				}
			$result .= '<option value="'.$options_value[$i].'"'.$sel.'>'.$options_label[$i].'</option>';
			}
		$result .= '</select>';
		return $result;
		}
		
	function space_create($row) {
		$result = "<br><br><b>".$row->options."</b><br>";
		return $result;
		}
	
	function lang_replacer($str) {
		$final = preg_replace_callback("/\[\[(_SCHEDULER_.*)\]\]/U", array($this, 'lang_callback'), $str);
		return $final;
		}
	
	function lang_callback($matches) {
		if (isset($this->scheduler_lang[$matches[1]])) {
			return $this->scheduler_lang[$matches[1]];
			}
			else {
			return $matches[0];
			}
		}
}

?>